﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

// 导出地表的UV匹配表
[ExecuteInEditMode]
public class TileUVExporter : MonoBehaviour
{

    private class IndexInfo
    {
        public Vector4 uv;
        public string spriteName;
    }

    public enum E_BuildingDirection
    {
        Up = 1,
        Down = 2,
        Left = 3,
        Right = 4,
    }

    private const string configPath = "/../Lua/modules/city/config/";

    public string LuaFileName = "TileUVConfig";
    
    public Texture2D spriteAtlas;

    public string spriteSubName = "lc_public_ground";

    public int spriteTypeNum = 3;

    public float floatOffset = 0f;

    private Dictionary<int,List<IndexInfo>> UVTable = new Dictionary<int, List<IndexInfo>>();
    
    private Dictionary<string,Rect> uvRectDic = new Dictionary<string, Rect>();

    public void SpawnConfig()
    {

#if UNITY_EDITOR
        UVTable.Clear();
        uvRectDic.Clear();
        
        string texturePath = AssetDatabase.GetAssetPath(spriteAtlas.GetInstanceID());

        TextureImporter importer = (TextureImporter) AssetImporter.GetAtPath(texturePath);
 
        for(int i=0;i < importer.spritesheet.Length;i++)
        {
            Rect uv = new Rect();

            uv.x = importer.spritesheet[i].rect.x / spriteAtlas.width + floatOffset;
            uv.y = importer.spritesheet[i].rect.y / spriteAtlas.height + floatOffset;
            
            uv.width = importer.spritesheet[i].rect.width/spriteAtlas.width - floatOffset;
            uv.height = importer.spritesheet[i].rect.height/spriteAtlas.height - floatOffset;

            uvRectDic[importer.spritesheet[i].name] = uv;
            //change pixel coords to uvs.
        }

        
//        -- 建筑间的关系
//        E_BuildingRelationCode = {
//            None = 0,
//            Top = 1,
//            Left = 2,
//            Bottom = 4,
//            Right = 8,
//        }

//
//        if dir == E_BuildingDirection.Right then
//            euler.z = euler.z - 90
//        elseif dir == E_BuildingDirection.Down then
//            euler.z = euler.z - 180
//        elseif dir == E_BuildingDirection.Left then
//            euler.z = euler.z - 270
//        end

        for (int i = 1; i <= spriteTypeNum; i++)
        {
            UVTable[i] = new List<IndexInfo>();

            UVTable[i].Add(GetUVLB2RT(i,16,E_BuildingDirection.Up));       // 0 None
            UVTable[i].Add(GetUVLB2RT(i,13,E_BuildingDirection.Up));     // 1 TOP
            UVTable[i].Add(GetUVLB2RT(i,12,E_BuildingDirection.Up));     // 2 Left
            UVTable[i].Add(GetUVLB2RT(i,5,E_BuildingDirection.Up));     // 3 TOP + Left
            UVTable[i].Add(GetUVLB2RT(i,15,E_BuildingDirection.Up));       // 4 Bottom
            UVTable[i].Add(GetUVLB2RT(i,4,E_BuildingDirection.Up));    // 5 TOP + Bottom
            UVTable[i].Add(GetUVLB2RT(i,6,E_BuildingDirection.Up));     // 6 Left + Bottom
            UVTable[i].Add(GetUVLB2RT(i,9,E_BuildingDirection.Up));    // 7 Top + Left + Bottom
            UVTable[i].Add(GetUVLB2RT(i,14,E_BuildingDirection.Up));       // 8 Right
            UVTable[i].Add(GetUVLB2RT(i,7,E_BuildingDirection.Up));    // 9 Right + Top
            UVTable[i].Add(GetUVLB2RT(i,2,E_BuildingDirection.Up));    // 10 Left + Right
            UVTable[i].Add(GetUVLB2RT(i,10,E_BuildingDirection.Up));     // 11 Top + Left + Right
            UVTable[i].Add(GetUVLB2RT(i,8,E_BuildingDirection.Up));  // 12 Bottom + Right
            UVTable[i].Add(GetUVLB2RT(i,11,E_BuildingDirection.Up));    // 13 Top + Bottom + Right
            UVTable[i].Add(GetUVLB2RT(i,1,E_BuildingDirection.Up));    // 14 Left+ Right + Bottom
            UVTable[i].Add(GetUVLB2RT(i,3,E_BuildingDirection.Up));    // 15
            

//            UVTable[i].Add(GetUVLB2RT(i,1,E_BuildingDirection.Up));       // 0 None
//            UVTable[i].Add(GetUVLB2RT(i,2,E_BuildingDirection.Down));     // 1 TOP
//            UVTable[i].Add(GetUVLB2RT(i,2,E_BuildingDirection.Left));     // 2 Left
//            UVTable[i].Add(GetUVLB2RT(i,4,E_BuildingDirection.Down));     // 3 TOP + Left
//            UVTable[i].Add(GetUVLB2RT(i,2,E_BuildingDirection.Up));       // 4 Bottom
//            UVTable[i].Add(GetUVLB2RT(i,3,E_BuildingDirection.Right));    // 5 TOP + Bottom
//            UVTable[i].Add(GetUVLB2RT(i,4,E_BuildingDirection.Left));     // 6 Left + Bottom
//            UVTable[i].Add(GetUVLB2RT(i,6,E_BuildingDirection.Left));    // 7 Top + Left + Bottom
//            UVTable[i].Add(GetUVLB2RT(i,2,E_BuildingDirection.Right));       // 8 Right
//            UVTable[i].Add(GetUVLB2RT(i,4,E_BuildingDirection.Right));    // 9 Right + Top
//            UVTable[i].Add(GetUVLB2RT(i,3,E_BuildingDirection.Up));    // 10 Left + Right
//            UVTable[i].Add(GetUVLB2RT(i,6,E_BuildingDirection.Down));     // 11 Top + Left + Right
//            UVTable[i].Add(GetUVLB2RT(i,4,E_BuildingDirection.Up));  // 12 Bottom + Right
//            UVTable[i].Add(GetUVLB2RT(i,6,E_BuildingDirection.Right));    // 13 Top + Bottom + Right
//            UVTable[i].Add(GetUVLB2RT(i,6,E_BuildingDirection.Up));    // 14 Left+ Right + Bottom
//            UVTable[i].Add(GetUVLB2RT(i,5,E_BuildingDirection.Up));    // 15
            
        }


        string path = Application.dataPath + configPath;

        string filePath = path + LuaFileName + ".lua";
        if (!Directory.Exists (path)) {
            Directory.CreateDirectory (path);
        }
        StringBuilder sb = new StringBuilder ();
        sb.AppendLine ("return {");


        foreach (var uvTable in UVTable)
        {
            for (int i = 0; i < uvTable.Value.Count; i++)
            {
                int index = (uvTable.Key - 1) * 16 + i + 1;

                Vector4 col = uvTable.Value[i].uv;
                
                sb.Append(string.Format ("[{0}] = {{\n color = Color.New({1},{2},{3},{4}),\n spriteName = \"{5}\"\n}}\n,",index,col.x,col.y,col.z,col.w,uvTable.Value[i].spriteName));
            }
            
            
        }
        
        
        
        sb.AppendLine ("}");

        StreamWriter streamWriter = new StreamWriter (filePath);
        streamWriter.Write (sb.ToString ());
        streamWriter.Flush ();
        streamWriter.Close ();

        AssetDatabase.Refresh ();
        Debug.Log ("导出数据成功：" + filePath);

#endif
     
    }



    private IndexInfo GetUVLB2RT(int typeIndex,int imageIndex,E_BuildingDirection dir)
    {
        string spriteName = string.Format("{0}_{1}_{2}", spriteSubName, typeIndex, imageIndex);
        Rect uvRect = uvRectDic[spriteName];
        
        // 左下角
        Vector2 leftBottom = new Vector2(uvRect.x, uvRect.y);
        Vector2 leftTop = new Vector2(uvRect.x,uvRect.y + uvRect.height);
        Vector2 rightBottom = new Vector2(uvRect.x + uvRect.width, uvRect.y);
        Vector2 rightTop = new Vector2(uvRect.x + uvRect.width ,uvRect.y + uvRect.height);
        
        Vector4 v = new Vector4(leftBottom.x, leftBottom.y, rightTop.x, rightTop.y);

        v.z *= 0.1f;
        v.z += typeIndex;
        
        v.w *= 0.1f;
        // 根据旋转方向选择左下角、右上角
        if (dir == E_BuildingDirection.Up)
        {
            v.w += 0;
        }

        if (dir == E_BuildingDirection.Right)
        {
            v.w += 270;
        }

        if (dir == E_BuildingDirection.Down)
        {
            v.w += 180;
        }

        if (dir == E_BuildingDirection.Left)
        {
            v.w += 90;
        }
        
        
        return new IndexInfo()
        {
            uv = v,
            spriteName = spriteName
        };
    }
    
}